package cn.com.oceansoft.business.os.controller;

import cn.com.oceansoft.business.base.controller.BaseController;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.os.entity.Choose;
import cn.com.oceansoft.business.os.service.IChooseService;
import cn.com.oceansoft.nos.cache.redis.CacheBaseService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import static org.springframework.web.bind.annotation.RequestMethod.DELETE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by zhangxl on 2016/12/30.
 */

@Controller
@RequestMapping("choose")
public class ChooseController extends BaseController {

    @Resource
    private CacheBaseService cacheBaseService;

    @Resource
    private IChooseService chooseService;

    @RequestMapping(value = "index", method = GET)
    public String openChooseIndex(Query query, Model model) {
        model.addAttribute("query", query);
        model.addAttribute("pager", chooseService.findPageByCondition(query));
        return "os/choose/choose";
    }

    @RequestMapping(value = "list", method = POST)
    public String loadChooseByPage(Query query, Model model) {
        model.addAttribute("query", query);
        model.addAttribute("pager", chooseService.findPageByCondition(query));
        return "os/choose/choose";
    }

    @ResponseBody
    @RequestMapping(value = "del/{regid}", method = DELETE)
    public Result delChoose(@PathVariable("regid") String id) {
        chooseService.delete(id);
        return new Result();
    }

    @RequestMapping(value = "new", method = GET)
    public String openAddChoose() {
        return "os/choose/choose-new";
    }

    @ResponseBody
    @RequestMapping(value = "new", method = POST)
    public Result addChoose(Choose choose) {
        int status = choose.getStatus();
        cacheBaseService.cache("status", status);
        return chooseService.save(choose);
    }

    @RequestMapping(value = "edit/{choose-id}", method = GET)
    public String gotoChooseEdit(@PathVariable("choose-id") int id, Model model) {
        Choose choose = chooseService.findChooseById(id);
        model.addAttribute("choose", choose);
        return "os/choose/choose-edit";
    }

    @ResponseBody
    @RequestMapping(value = "edit", method = POST)
    public Result editChoose(Choose choose) {
        int status = choose.getStatus();
        cacheBaseService.cache("status", status);
        return chooseService.update(choose);
    }
}